#ifndef _CHUNK_H_
#define _CHUNK_H_

#include"Common.h"
#include"Block.h"
#include"Debug.h"

#include<thirdparty\glew.h>
#include<memory>
#include<string>
#include<vector>

namespace Craft{

	const int chunk_width = 16;
	const int max_count = 4096;

	class World;

	/*
	@brief
	*/
	class Chunk
	{
	public:
		Chunk(int id,World* world):
			m_world(world),
			m_Id(id),
			m_block_counts(0),
			m_width(chunk_width),
			m_max_counts(max_count),
			m_pos(0)
		{
		}

		~Chunk()
		{
		}

		Block& get_block(const glm::vec3& pos);
		Block& get_block_byWorldPos(const glm::vec3& pos);
		void   set_block(Block block, const glm::vec3& pos,bool updataAO = false, bool updateLight = false);
		void   set_block_byWorldPos(Block block, const glm::vec3& pos, bool updataAO = false, bool updateLight = false);
		void   destory_block(const glm::vec3& pos,bool updataAO = false, bool updateLight = false);

		const glm::vec3 get_pos()const;
		const int get_width()const;
		const int get_count()const;
		const int get_id()const;

		void   set_world(World* world);
		World* get_world();

		void set_pos(const glm::vec3& pos);
		void set_update(bool bupdate);

		bool is_update()const;

		Block intersection(const glm::vec3& orgin, const glm::vec3& dir, GLfloat range, HitInfo& hitInfo);

		void  get_hitBoxes(const AABB& box,std::vector<AABB>& hitBoxes);

		void save_chunks();
		void load_chunks();

		// light // no use 
		void update_blockLight(GLuint skyLight);
		void injection(const glm::vec3& pos, GLuint skyLight);
		void propagation(const glm::vec3& pos);

	private:

		World* m_world;

		int m_Id;
		int m_width;
		int m_max_counts;
		int m_block_counts;

		glm::vec3 m_pos;

		bool  m_update = false;

		Block m_blocks[max_count];
		// quad tree to manger?

	};


	/*
		@brief chunks管理类，用以管理一系列chunk，功能和普通chunk一致
			   不过存在范围内选择对应的Chunk执行相应操作
	*/
	class ChunkManger
	{
	public:

	};

}

#endif